<template>
  <div class="app-container">
    <div class="customContainer ">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <span v-if="formInline.type==1">采购渠道：供应商</span>
          <span v-else>采购渠道：中央仓</span>
        </el-form-item>
        <dj-store-select :category="547" :needItem="true" :defaultStore="formInline.store_id" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy-MM-dd" type="daterange" align="right" unlink-panels range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
      </el-form>
      <!-- 表格 :summary="true"-->
      <dj-simple-table :columns="tableColumns" :data="listData" :global="global" :loading="listLoading" />


    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber">
      </el-pagination>
      <span v-if="listData.length > 0" class="pull-right mt10" style="font-size:14px;color:#606266">采购入库数量合计: {{num}}件 　采购入库金额合计: {{cost_totle|keepTwoNum}}元</span>
    </div>
  </div>
</template>
<script>
import { getPurchaseList, getPurchaseListPage, getPurchaseSummary } from '@/api/report/store_stock_in';
import { DjSimpleTable } from '@/components/table';
import { DjStoreSelect } from '@/components/store';
import { pickerOptionsF, pickerDateDot, optionMaxTime, getDateString } from '@/utils';
export default {
  components: {
    'dj-simple-table': DjSimpleTable,
    'dj-store-select': DjStoreSelect
  },
  data() {
    return {
      global: {
        store_name: ''
      },
      tableColumns: [
        {
          label: '序号',
          dtcol: 'index'
        },
        {
          label: '收货单号',
          dtcol: 'send_order_no',
          link: 'report/stock_in_receive_detail'
        },
        {
          label: '批次号',
          dtcol: 'no_name',
          link: 'report/stock_in_receive_batch_detail'
        },
        {
          label: '采购渠道',
          dtcol: 'type_name'
        },
        {
          label: '入库时间',
          dtcol: 'time'
        },
        {
          label: '本次入库数量',
          dtcol: 'arrive_num'
          // summary: true
        },
        {
          label: '本次入库金额',
          dtcol: 'arrive_money',
          isnum: true
          // summary: true
        }
      ],
      // arrive_money: "18.0000"
      // arrive_num: "2"
      // create_time: "1573133444"
      // id: "50"
      // no: "1"
      // no_name: "第1批次"
      // send_order_no: "SHLA0000000050"
      // time: "2019-11-07"
      // type: "1"
      // type_name: "供应商"
      listData: [], // 对象数组元素中的对象含有store_id属性
      listLoading: false,
      formInline: {
        store_id: this.$store.getters.optionId,
        start_time: '',
        end_time: '',
        type: 1, // 1供应商采购入库，2中央仓采购入库，3其他仓调拨入库
        look_del: '0'
      },
      pickerOptions: pickerOptionsF(-1),
      pickerDate: pickerDateDot(-1,-1),
      currentPage: 1,
      totalDataNumber: 0,
      num: 0,
      cost_totle: 0
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.beforeComingIn();
    });
  },
  methods: {
    beforeComingIn() {
      if (this.$route.query.routerflag) {
        // console.log(this.$route.query, 'zst1119 beforeComingIn route.query');
        let col = this.$route.query.col;
        if (col == 'supplier_money') {
          this.formInline.type = 1;
        } else {
          this.formInline.type = 2;
        }
        // 本月第一天至昨天
        // let fist_day = getDateString('this_month_first_day');
        // let yesterday = getDateString('yesterday');
        let start_time = this.$route.query.global.start_time;
        let end_time = this.$route.query.global.end_time;
        this.pickerDate = [start_time, end_time];
        this.formInline.store_id = this.$route.query.row.store_id;
        // this.searchClick();
      } else {
        if (!this.listData || this.listData.length == 0) {
          this.searchClick();
        }
      }
    },
    selectStoreChanged(obj, item) {
      for (let key in obj) this.formInline[key] = obj[key];
      this.global.store_name = item.name;
      this.searchClick();
    },
    // 搜索
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start_time = this.pickerDate ? this.pickerDate[0] : '';
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
        this.fetchData();
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPurchaseListPage(this.formInline)
        .then(response => { 
          this.totalDataNumber = response.message.page * 20;
          // 列表数据
          getPurchaseList(this.formInline, this.currentPage).then(response => {
            if (response.code == 1 && response.message && response.message.length > 0) {
              response.message.forEach((e, index) => {
                e.index = index + 1 +(this.currentPage - 1) * 20;
              });
              this.listData = response.message;
            } else {
              this.listData = [];
            }
            // this.list = response.data;
            this.listLoading = false;
          });
          // 合计
          getPurchaseSummary(this.formInline).then(response => {
            this.num = response.data.arrive_num ? response.data.arrive_num : 0;
            this.cost_totle = response.data.arrive_money;
          });

        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true; 
      this.currentPage = val
      getPurchaseList(this.formInline, this.currentPage)
        .then(response => {
          if (response.code == 1 && response.message && response.message.length > 0) {
            response.message.forEach((e, index) => { 
              e.index = index + 1 +(this.currentPage - 1) * 20;
            });

            this.listData = response.message;
          } else {
            this.listData = [];
          }
          // this.list = response.data;
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
        });
      // 合计
      getPurchaseSummary(this.formInline).then(response => {
        this.num = response.data.arrive_num ? response.data.arrive_num : 0;
        this.cost_totle = response.data.arrive_money;
      });
    }
  }
};
</script>